<?php
    include("include/header.inc.php");
    include("include/func.inc.php");
    $_SESSION['page'] = 'event';
    
    include("include/sendMail.inc.php");
    
    //Function to display message when event not found
    function event_not_found($texts){
        echo '<p align="center">'.$texts['event_not_found'][$_SESSION['lang']].'<br/><br/>';
        echo '<a href="./events.php" rel="external" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
    }
    
    //Function to display content if not theme found
    function display_theme_not_found($texts){
        echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
        echo '<div class="tableauPage">';
        // Only the "event" parameter exists in the url
        if(isset ($_GET['event'])){
            //Error : theme not found
            echo '<p align="center">'.$texts['theme_not_found'][$_SESSION['lang']].'<br/><br/>';
            echo '<a href="./detailEvent.php?event='.$_GET['event'].'" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
        }
        else {
            //Error : event not found
             event_not_found($texts);
        }
        echo '</p></div>';
    }
    
    echo '<div data-role="content">';

    //Check that the "theme" parameter exist in the url
    if(isset ($_GET['theme'])){
        
        //Current theme id
        $id_theme = $_GET['theme'];
        
        //Username of the current user
        $username = strtoupper($_SESSION['login']);
        
        //Variable to stock the confirmation message
        $confirmationMessage = '';
        
        //SAP connection
        $rfc = connectSAP();
        
        //-- FUNCTION MODULE: Z_FM_M646_GET_THEMES --
        //We must know if the function really exists
        $fce_get_themes = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
        if(!$fce_get_themes){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }

        //Pass import parameters
        saprfc_import ($fce_get_themes,"IV_THEME",$_GET['theme']);

        //Pass table parameters
        saprfc_table_init ($fce_get_themes,"TAB_DETAILS_THEME");

        //Call and execute the function
        $rc_get_themes = saprfc_call_and_receive ($fce_get_themes);            
        if ($rc_get_themes != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_get_themes));
            else 
                echo ("Call error: ".saprfc_error($fce_get_themes));
            exit;
        }

        //Fetch the data from the internal tables
        $data_row = saprfc_table_rows ($fce_get_themes,"TAB_DETAILS_THEME");
        if ($data_row > 0){//If these theme exist
            $DATARow = saprfc_table_read ($fce_get_themes,"TAB_DETAILS_THEME",1);

            //-- FUNCTION MODULE: Z_FM_M646_REGISTRATION_CANCEL --
            //We must know if the function really exists
            $fce_registration_cancel = saprfc_function_discover($rfc, "Z_FM_M646_REGISTRATION_CANCEL");
            if(!$fce_registration_cancel){
                echo "The function module has failed.";
                echo $rfc;
                exit;
            }

            //Pass import parameters
            saprfc_import ($fce_registration_cancel,"IV_THEME", $id_theme);
            saprfc_import ($fce_registration_cancel,"IP_USERNAME", $username);

            //Call and execute the function
            $rcRegistration = saprfc_call_and_receive ($fce_registration_cancel);            
            if ($rcRegistration != SAPRFC_OK){
                if ($rfc == SAPRFC_EXCEPTION )
                    echo ("Exception raised: ".saprfc_exception($fce_registration_cancel));
                else
                    echo ("Call error: ".saprfc_error($fce_registration_cancel));
                exit;
            }
            
            //realease the function
            saprfc_function_free($fce_registration_cancel);
            
            // Message to confirm the registration

            //Variables to display the date in a correct format
            $dateStart = substr($DATARow['STARTDATE'],-2).'.'.substr($DATARow['STARTDATE'],4,2).'.'.substr($DATARow['STARTDATE'],0,4);
            $dateEnd = substr($DATARow['ENDDATE'],-2).'.'.substr($DATARow['ENDDATE'],4,2).'.'.substr($DATARow['ENDDATE'],0,4);
            $timeStart = substr($DATARow['STARTTIME'],0,2).':'.substr($DATARow['STARTTIME'],2,2);
            $timeEnd = substr($DATARow['ENDTIME'],0,2).':'.substr($DATARow['ENDTIME'],2,2);
            
            //Confirmation
            $confirmationMessage.= '<b>'.$texts['unregistered_to_the_theme'][$_SESSION['lang']].'</b>';
            $confirmationMessage.= '<h3>'.$texts['theme'][$_SESSION['lang']].' '.$DATARow['TITLE_THEME'].'</h3>';
            $confirmationMessage.= '<div style="margin-left:10px;">';
                //Dates
                $confirmationMessage.= $texts['start_date'][$_SESSION['lang']].' '.$dateStart;
                $confirmationMessage.= ' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeStart.'<br/>';
                $confirmationMessage.= $texts['end_date'][$_SESSION['lang']].' '.$dateEnd;
                $confirmationMessage.= ' a '.$timeEnd.'<br/>';
            $confirmationMessage.= '</div>'; 
            
            if (isset($_SESSION['mail']))
                sendMail($confirmationMessage,$_SESSION['mail'],'unregister');

            //Title
            echo '<h2>'. $texts['confirmation'][$_SESSION['lang']] .'</h2>';
            echo '<div class="tableauPage">';
            echo '<p><b>'. $texts['unregistered_to_theme_conf'][$_SESSION['lang']] .'</b></p>';
            echo '<h3>'.$texts['theme'][$_SESSION['lang']].' '.$DATARow['TITLE_THEME'].'</h3>';
            echo '<div style="margin-left:10px;">';
            echo $texts['start_date'][$_SESSION['lang']].' '.$dateStart.' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeStart.'<br/>';
            echo $texts['end_date'][$_SESSION['lang']].' '.$dateEnd.' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeEnd.'<br/>';
            echo '</div>'; 
            echo '</br><hr align="center" width="75%" size=1 color="red" />';
            echo '<p align="center"><a href="./index.php" data-role="button" data-inline="true">'.$texts['home'][$_SESSION['lang']].'</a></p>';
            echo '</div>';
        }
        else//These theme does not exist
            display_theme_not_found($texts);
        
        //realease the function and close the connection
        saprfc_function_free($fce_get_themes);
        saprfc_close($rfc);
    }
    else//Theme and themes doesn't exist
        display_theme_not_found($texts);
    
    echo '</div>'; 
    include('include/footer.inc.php');  // Call footer
?>